<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class PrivateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $agent_id = $request -> cookie('logon')['agent_id'];
        //查询图片表
        $data = DB::table('image') -> where('agent_id',$agent_id) -> get();
         return view('admin/private/index',['data'=>$data,'agent_id'=>$agent_id]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getTest()
    {
        return view('admin/private/test');
    }

    public function postAction(Request $request)
    {
        $file = $_FILES['test'];
        $agent_id = $request -> cookie('logon')['agent_id'];
        $data['msg'] = '';
        $data['img'] = [];
        if(is_array($file['name'])){
            foreach ($file['name'] as $k=>$v){
                if($file['error'][$k] == 0){
                    if($file['size'][$k] < 2*1024*1024){
                        //获取文件临时路径
                        $tmp_name = $file['tmp_name'][$k];
                        $pathInfo = pathinfo($v);
                        $ext = $pathInfo['extension'];
                        //图片上传
                        $res = $this -> imgUp($tmp_name,$ext,$agent_id);
                        array_push($data['img'],$res);
                    }else{
                        $data['msg'] .= $file['name'][$k].',';
                    }
                }
            }
        }
        return json_encode($data);
    }

    //图片上传方法
    public function imgUp($tmp_name,$ext,$agent_id){
        $res = [];
        //动态生成目标路径
        $tar_path = $_SERVER['DOCUMENT_ROOT'].'/pc/private/img/agent_'.$agent_id;
        //如果目录不存在 ,创建目录
        if(!file_exists($tar_path)) {
            mkdir($tar_path);
        }
        $path_name = time().rand(1000,9999).'.'.$ext;
        //将图片移入目标文件
        $bool = move_uploaded_file($tmp_name,$tar_path.'/'.$path_name);
        if($bool){
            //将图片路径写入数据库
            $id = DB::table('image') -> insertGetId(['name'=>$path_name,'agent_id'=>$agent_id]);
            if(!$id){
                unlink($tar_path.'/'.$path_name);
            }else{
                $res = ['img_id'=>$id,'img_name'=>'agent_'.$agent_id.'/'.$path_name];
            }
        }
        return $res;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        //修改头像
        //修改代理商下所有图片状态为0
        DB::table('image') -> where('agent_id',$request -> cookie('logon')['agent_id']) -> update(['status'=>0]);
        //修改$id图片的状态为1
        $res = DB::table('image') -> where('id',$id) -> update(['status'=>1]);
        if($res){
            $data['status'] = '1';
        }else{
            $data['status'] = '-1';
        }
        echo json_encode($data);
    }

    //删除图片
    public function postDestroy(Request $request){
        $id = $request -> input('id');
        $agent_id = $request -> cookie('logon')['agent_id'];
            //删除数据
            $status = DB::table('image') -> where('id',$id) -> value('status');
            if(!$status){
                //查找图片路径并删除图片
                $name = DB::table('image') -> where('id',$id) -> value('name');
                $tar_name = $_SERVER['DOCUMENT_ROOT'].'/pc/private/img/agent_'.$agent_id.'/'.$name;
                $bool = unlink($tar_name);
                if($bool) {
                    $res = DB::table('image')->delete($id);
                    if ($res) {
                        $data['status'] = '1';
                    } else {
                        $data['status'] = '-1';
                    }
                }else{
                        $data['status'] = '-1';
                }
            }else{
                $data['status'] = '-1';
            }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
